package com.zhang.search.listener;

import com.alibaba.fastjson.JSON;
import com.zhang.common.dto.UpdateSkuSaleCountDTO;
import com.zhang.search.service.IProductElasticSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class ModifySkuSaleCountListener {
    @Autowired
    private IProductElasticSearchService productElasticSearchService;

    /**
     * 修改sku的销售量
     *
     * @param json
     */
    @RabbitListener(queues = "sku.sale.count.elasticsearch.queue")
    public void listenSkuSaleCountQueueMessage(String json) {
        UpdateSkuSaleCountDTO updateSkuSaleCountDTO = JSON.parseObject(json, UpdateSkuSaleCountDTO.class);
        try {
            productElasticSearchService.updateSkuSaleCount(updateSkuSaleCountDTO.getSkuId(), updateSkuSaleCountDTO.getSaleCount());
        } catch (IOException e) {
            log.error("修改销售量失败");
            throw new RuntimeException(e);
        }
    }
}
